import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from './components/login/Login.vue';
import AdminSystem from './components/AdminSystem.vue';
import Buildings from '@/components/buildings/Buildings.vue';
import AddBuildings from '@/components/buildings/AddBuildings.vue';
import UpdateBuildings from '@/components/buildings/UpdateBuildings.vue';
import Unit from '@/components/unit/Unit.vue';
import AddUnit from '@/components/unit/AddUnit.vue';
import UpdateUnit from '@/components/unit/UpdateUnit.vue';
import store from "@/store";
import Dormitories from "@/components/dormitories/Dormitories.vue";
import AddDormitories from "@/components/dormitories/AddDormitories.vue";
import UpdateDormitories from "@/components/dormitories/UpdateDormitories.vue";
import BaseSetting from "@/components/settings/BaseSetting.vue";
import Security from "@/components/settings/Security.vue";
import DormitoryAllocation from "@/components/dormitoryAllocation/DormitoryAllocation.vue";
import AddDormitoryAllocation from "@/components/dormitoryAllocation/AddDormitoryAllocation.vue";
import UpdateDormitoryAllocation from "@/components/dormitoryAllocation/UpdateDormitoryAllocation.vue";
import Users from "@/components/users/Users.vue";
import AddUsers from "@/components/users/AddUsers.vue";
import FOF from "@/components/404/404.vue";

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        redirect: '/login' // 可以设置默认跳转到登录页
    },
    // 登录
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    // 主要的页面（左侧导航栏 和右边的主要区域）
    {
        path: '/AdminSystem',
        name: 'AdminSystem',
        component: AdminSystem,
        // 要实现跳转必须把子路由写在这里，不然不会生效
        children:
            [
                {
                    path: "/buildings",
                    name: "Buildings",
                    component: Buildings,
                },
                {
                    path: "/buildings",
                    name: "AddBuildings",
                    component: AddBuildings,
                },
                {
                    path: "/buildings",
                    name: "UpdateBuildings",
                    component: UpdateBuildings,
                },
                {
                    path: "/unit",
                    name: "Unit",
                    component: Unit,
                },
                {
                    path: "/unit",
                    name: "AddUnit",
                    component: AddUnit,
                },
                {
                    path: "/unit",
                    name: "UpdateUnit",
                    component: UpdateUnit,
                },
                {
                    path: "/dormitories",
                    name: "Dormitories",
                    component: Dormitories,
                },
                {
                    path: "/dormitories",
                    name: "AddDormitories",
                    component: AddDormitories,
                },
                {
                    path: "/dormitories",
                    name: "UpdateDormitories",
                    component: UpdateDormitories,
                },
                {
                    path: "/settings",
                    name: "BaseSetting",
                    component: BaseSetting,
                },
                {
                    path: "/settings",
                    name: "Security",
                    component: Security,
                },
                {
                    path: "/dormitoryAllocation",
                    name: "DormitoryAllocation",
                    component: DormitoryAllocation,
                },
                {
                    path: "/dormitoryAllocation",
                    name: "AddDormitoryAllocation",
                    component: AddDormitoryAllocation,
                },
                {
                    path: "/dormitoryAllocation",
                    name: "UpdateDormitoryAllocation",
                    component: UpdateDormitoryAllocation,
                },
                {
                    path: "/users",
                    name: "Users",
                    component: Users,
                }
                ,
                {
                    path: "/users",
                    name: "AddUsers",
                    component: AddUsers,
                },
                {
                    path: "/FOF",
                    name: "FOF",
                    component: FOF,
                }
            ]
    }
    // 在这里添加其他路由
];

const router = new VueRouter({
    mode: 'history', // 使用 HTML5 History 模式
    routes
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const token = store.state.token; // 假设你的 token 存储在 Vuex 的 state 中，你可以根据实际情况修改
    if (token || to.path === '/login') {
        // 如果 token 存在，或者用户正在访问登录页面，则放行
        next();
    } else {
        // 如果 token 不存在且用户尝试访问非登录页面，则重定向到登录页
        next('/login');
    }
});

export default router;
